<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "mydb"
 
//创建链接
$conn = new mysqli($servername, $username, $password, $dbname);
//检测连接
if ($conn->connect_error){
    die("连接失败:" $conn->connect_error );
}

//sql 插入数据的语句
$sql = "INSERT INTO MyGueste (firstname,lastname,email)
VALUES （'John','Doe','john@example.com'）";


if ($conn->query($sql) === TRUE) {
    echo "新纪录插入成功";
}else{
    echo "Error : " .sql . "<br>" . $conn->error;
}


?>
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "mydb";

//创建连接
$conn = mysqli_connect($servername,$username,$password,$dbname);
//检测连接
if (!$conn) {
    die("Connection failed:" . mysqli_connect_error());
    
}

// sql 插入数据的语句
$sql = "INSERT INTO MyGuests (firstname,lastname,email)
VALUES ('John','Doe','john@example.com')";

if (mysqli_query($conn,$sql)) {

    echo"新纪录插入成功";

}else {
    echo "Error:" . $sql ."<br>" .mysqli_($conn);
}
//关闭连接


try {
    $conn = new PDO("mysql:host=$servername;dbname=$dbname",$username,$password);
    //设置 PDO 错我模式，用于抛出异常
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    //sql 插入数据的语句
    $sql = "INSERT INTO MyGuests (firstname,lastname,email)
    VALUES ('john','Doe','john@example.com')";

    //使用exec(),没有结果返回
    $conn->exec($sql);
    echo"新纪录插入成功";
}
 catch (PDOException $e) {
     echo $sql . "<br>" . $e->getMessage();
}
//关闭连接
$conn = null;




?>




